package com.gitee.hengboy.job.server.configuration;

import com.gitee.hengboy.distributed.job.configuration.JobBasicConfiguration;
import com.gitee.hengboy.job.server.configuration.properties.JobServerProperties;
import com.weibo.api.motan.config.springsupport.BasicServiceConfigBean;
import com.weibo.api.motan.config.springsupport.RegistryConfigBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * job server config
 *
 * @author：于起宇 <p>
 * ================================
 * Created with IDEA.
 * Date：2019-01-07
 * Time：15:19
 * 个人博客：http://blog.yuqiyu.com
 * 简书：http://www.jianshu.com/u/092df3f77bca
 * 码云：https://gitee.com/hengboy
 * GitHub：https://github.com/hengyuboy
 * ================================
 * </p>
 */
@Configuration
@EnableConfigurationProperties(JobServerProperties.class)
@Import(JobBasicConfiguration.class)
public class JobServerConfiguration {
    /**
     * job server properties
     */
    private JobServerProperties jobServerProperties;

    public JobServerConfiguration(JobServerProperties jobServerProperties) {
        this.jobServerProperties = jobServerProperties;
    }

    /**
     * basic service config
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public BasicServiceConfigBean basicServiceConfig() {
        BasicServiceConfigBean basicServiceConfigBean = new BasicServiceConfigBean();
        basicServiceConfigBean.setGroup(jobServerProperties.getGroup());
        basicServiceConfigBean.setExport(jobServerProperties.getProtocol() + ":" + jobServerProperties.getRegPort());
        basicServiceConfigBean.setRegistry(jobServerProperties.getRegistryName());
        return basicServiceConfigBean;
    }


    /**
     * job 注册中心配置
     *
     * @return
     */
    @Bean("jobRegistry")
    @ConditionalOnMissingBean
    public RegistryConfigBean registryConfigBean() {
        RegistryConfigBean registryConfigBean = new RegistryConfigBean();
        registryConfigBean.setRegProtocol(jobServerProperties.getRegProtocol());
        return registryConfigBean;
    }
}
